﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ThrowArror : MonoBehaviour
{
    [Header("箭")] public GameObject arrow;
    [Header("基础力")] public float baseForce = 20f;
    [Header("最大力")] public float maxForce = 40f;
    [Header("蓄力系数")] public float holdSen = 0.2f;

    public float nowForce = 0f;

    private Vector3 startPos;

    private void Start()
    {
        startPos = arrow.transform.localPosition;
    }

    public void StarThrow()
    {

        nowForce = baseForce;
    }
    /// <summary>
    /// 蓄力
    /// </summary>
    /// <param name="arr"></param>
    /// <param name="hs"></param>
    public void XuLi(GameObject arr, float hs)
    {
        nowForce += Time.deltaTime * hs;
        nowForce = Mathf.Clamp(nowForce, 0, maxForce);
        if (nowForce != maxForce)
        {
            arr.transform.Translate(Vector3.down * (1f * Time.deltaTime));

        }
    }

    /// <summary>
    /// 扔
    /// </summary>
    /// <param name="arr"></param>
    public void Throwing(GameObject arr)
    {

        var arrow = Instantiate(arr, arr.transform.parent);
        arrow.GetComponent<Rigidbody>().isKinematic = false;
        arrow.GetComponent<Collider>().enabled = true;
        arrow.GetComponent<Rigidbody>().AddForce(arrow.transform.up * nowForce, ForceMode.Impulse);
        arrow.transform.SetParent(null);

        nowForce = 0;
        this.arrow.transform.localPosition = startPos;
    }
}
